{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1fb3144c-99b8-482d-a205-0f50877653aa",
   "metadata": {},
   "source": [
    "# ReAct Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e8ac1778-0585-43c9-9dad-014d13d7460d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from llama_index.agent import ReActAgent\n",
    "from llama_index.llms import OpenAI, ChatMessage\n",
    "from llama_index.tools import BaseTool, FunctionTool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "26472aaf-1a12-49f9-9fe6-cbf41dd15f88",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def multiply(a: int, b: int) -> int:\n",
    "    \"\"\"Multiple two integers and returns the result integer\"\"\"\n",
    "    return a * b\n",
    "\n",
    "\n",
    "multiply_tool = FunctionTool.from_defaults(fn=multiply)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "df78ae85-bcf7-44c1-87ee-f301e646db20",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def add(a: int, b: int) -> int:\n",
    "    \"\"\"Add two integers and returns the result integer\"\"\"\n",
    "    return a + b\n",
    "\n",
    "\n",
    "add_tool = FunctionTool.from_defaults(fn=add)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b6c519f-6d74-4251-b38e-b8395a96d43b",
   "metadata": {},
   "source": [
    "## gpt-3.5-turbo-0613"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7ab300f1-b054-46d9-b1c8-dbcd0d538e5a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "llm = OpenAI(model=\"gpt-3.5-turbo-0613\")\n",
    "agent = ReActAgent.from_tools([multiply_tool, add_tool], llm=llm, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "69bb1aa9-1ea3-4c88-a4f3-239b76392aa5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[38;5;200m\u001b[1;3mThought: I need to use a tool to help me answer the question.\n",
      "Action: multiply\n",
      "Action Input: {'a': 2, 'b': 4}\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 8\n",
      "\u001b[0m\u001b[38;5;200m\u001b[1;3mThought: I have the result of the multiplication. Now I need to use the addition tool.\n",
      "Action: add\n",
      "Action Input: {'a': 20, 'b': 8}\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 28\n",
      "\u001b[0m\u001b[38;5;200m\u001b[1;3mResponse: 20 + 2 * 4 = 28\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "response = agent.chat(\"What is 20+2*4? Calculate step by step \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "76112bb6-a291-4235-ad00-d4f6ff20adfe",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[38;5;200m\u001b[1;3mThought: I need to use a tool to help me answer the question.\n",
      "Action: multiply\n",
      "Action Input: {'a': 2, 'b': 4}\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 8\n",
      "\u001b[0m\u001b[38;5;200m\u001b[1;3mThought: I need to use a tool to help me answer the question.\n",
      "Action: add\n",
      "Action Input: {'a': 20, 'b': 8}\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 28\n",
      "\u001b[0m20 + 2 * 4 = 28"
     ]
    }
   ],
   "source": [
    "response_gen = agent.stream_chat(\"What is 20+2*4? Calculate step by step\")\n",
    "response_gen.print_response_stream()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d112393-ac6e-45d4-a58e-fec9745a79fc",
   "metadata": {},
   "source": [
    "## gpt-4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1bb7d49b-404c-4a46-9a84-1f7bb8792991",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "llm = OpenAI(model=\"gpt-4\")\n",
    "agent = ReActAgent.from_tools([multiply_tool, add_tool], llm=llm, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "277f21d3-4f62-430e-b0fc-7561c64084d0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[38;5;200m\u001b[1;3mThought: I need to use the multiply tool first to calculate 2*4, then use the add tool to add the result to 2.\n",
      "Action: multiply\n",
      "Action Input: {'a': 2, 'b': 4}\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 8\n",
      "\u001b[0m\u001b[38;5;200m\u001b[1;3mThought: Now I need to use the add tool to add 2 to the result of the multiplication.\n",
      "Action: add\n",
      "Action Input: {'a': 2, 'b': 8}\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 10\n",
      "\u001b[0m\u001b[38;5;200m\u001b[1;3mResponse: 10\n",
      "\u001b[0m10\n"
     ]
    }
   ],
   "source": [
    "response = agent.chat(\"What is 2+2*4\")\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa9ffdae-962b-447d-a54a-624d5208aee8",
   "metadata": {
    "tags": []
   },
   "source": [
    "## text-davinci-003"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e402e2dc-0537-49d5-b399-3831114691e6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "llm = OpenAI(model=\"text-davinci-003\")\n",
    "agent = ReActAgent.from_tools([multiply_tool, add_tool], llm=llm, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f42bec92-5294-48a2-8a16-8a1d1a0cc908",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[38;5;200m\u001b[1;3mThought: I need to use a tool to help me answer the question.\n",
      "Action: multiply\n",
      "Action Input: {'a': 2, 'b': 4}\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 8\n",
      "\u001b[0m\u001b[38;5;200m\u001b[1;3mResponse: 10\n",
      "\u001b[0m10\n"
     ]
    }
   ],
   "source": [
    "response = agent.chat(\"What is 2+2*4\")\n",
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
